// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import store from './store'
import router from './router'
import Config from './config'
import initRouter from './router/menurouter'

import ElementUI from 'element-ui' // UI
import './assets/scss/element-custom.scss' //
import 'font-awesome/css/font-awesome.min.css'

import mavonEditor from 'mavon-editor'// md编辑器
import 'mavon-editor/dist/css/index.css'

import httpAPI from './utils/httpapi'
import {isNotNullORBlank} from "./utils/utils"
import './utils/filter_utils'
import arrUtil from "./utils/arrayutils"

//项目基本配置
Vue.config.productionTip = Config.production;

Vue.use(ElementUI);

Vue.use(mavonEditor);

for (const apiName in httpAPI) {
  if (httpAPI.hasOwnProperty(apiName)) {
    const element = httpAPI[apiName];
    Vue.prototype[apiName] = httpAPI[apiName];
  }
}
Vue.prototype.isNotNullORBlank = isNotNullORBlank;
Vue.prototype.arrUtil = arrUtil;

// router初始化配置
router.beforeEach((to, from, next) => {
  // 跳转如果是login，则直接跳转
  if (to.name == 'Login') {
    next();
    return;
  }
  var name = store.state.user.name;
  if (name == '未登录') {
    // 默认为未登录，跳转到login
    if (to.meta.requireAuth || to.name == null) {
      next({
        path: '/',
        query: {
          redirect: to.path
        }
      });
    } else {
      next();
    }
  } else {
    if(to.name == "Role"){
      next();
    }else{
      // 已经登陆，则加载所有路由并构建菜单
      initRouter(router, store);
      next();
     }
  }
})

// 启动vue实例
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
